package cn.xz.zgsg.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import cn.xz.zgsg.R
import cn.xz.zgsg.bean.DeviceList
import cn.xz.zgsg.bean.JsonRootBean
import cn.xz.zgsg.net.OkHttpUtil
import cn.xz.zgsg.util.AppHelper
import cn.xz.zgsg.util.U3dHttp

class DSJListAdapter1 : RecyclerView.Adapter<DSJListAdapter1.ChatViewHolder>() {
    private var mList: ArrayList<JsonRootBean>? = ArrayList()
    private var mContext: Context? = null
    private var mDevice: DeviceList? = null

    //设置列表适配器数据
    fun setList(mList: ArrayList<JsonRootBean>?, context: Context?,device: DeviceList?) {
        //赋值
        this.mList = mList
        //全局属性赋值
        mContext = context
        mDevice = device
        //刷新列表
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChatViewHolder {
        val view: View = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_dsj_list_layout, parent, false)
        return ChatViewHolder(view)
    }

    override fun onBindViewHolder(holder: ChatViewHolder, position: Int) {
        //获取当前条目数据
        val mData = mList!![position]
        //赋值
        holder.tvTxt.text = mData.title
        holder.tvTxt.background = AppHelper.getDrawable(R.mipmap.icon_ak)
        if (mData.interactable){
            holder.tvTxt.background = AppHelper.getDrawable(R.mipmap.bg_l)
        }

        holder.itemView.setOnClickListener {
            if (mData.interactable && mDevice != null){
                val url = "http://" + mDevice!!.ip +":8030/?act=unity&states=content&object=" + mData.id
                OkHttpUtil.post(url)
            }
        }
    }

    override fun getItemCount(): Int {
        return if (mList == null) 0 else mList!!.size
    }

    class ChatViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvTxt: TextView

        init {
            tvTxt = itemView.findViewById<View>(R.id.tv_txt) as TextView
        }
    }
}